package com.sinvo.oa.controller;


import com.sinvo.oa.entity.model.wechat.Menu;
import com.sinvo.oa.entity.vo.wechat.MenuVo;
import com.sinvo.oa.exception.CustomException;
import com.sinvo.oa.log.annotation.Log;
import com.sinvo.oa.log.enums.ServiceType;
import com.sinvo.oa.result.R;
import com.sinvo.oa.result.ResultCode;
import com.sinvo.oa.service.WechatMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 微信公众号菜单控制器
 */
@Api(tags = "微信菜单")
@RestController
@RequestMapping("admin/wechat/menu")
public class WechatMenuController {

    @Autowired
    private WechatMenuService menuService;

    /**
     * 根据id获取菜单
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('btn.menu.list')")
    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    public R get(@PathVariable Long id) {
        //验证参数
        if (StringUtils.isEmpty(id)) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        Menu menu = menuService.getById(id);
        return R.success(menu);
    }

    /**
     * 新增菜单
     * @param menu
     * @param result
     * @return
     */
    @Log(title = "微信菜单管理",businessType = ServiceType.INSERT)
    @PreAuthorize("hasAuthority('btn.menu.add')")
    @ApiOperation(value = "新增")
    @PostMapping("save")
    public R save(@Validated @RequestBody Menu menu, BindingResult result) {
        if (result.hasErrors()) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        menuService.saveMenu(menu);
        return R.success();
    }

    /**
     * 更新菜单
     * @param menu
     * @param result
     * @return
     */
    @Log(title = "微信菜单管理",businessType = ServiceType.UPDATE)
    @PreAuthorize("hasAuthority('btn.menu.update')")
    @ApiOperation(value = "修改")
    @PutMapping("update")
    public R updateById(@Validated @RequestBody Menu menu, BindingResult result) {
        if (result.hasErrors()) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        menuService.updateMenu(menu);
        return R.success();
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @Log(title = "微信菜单管理",businessType = ServiceType.DELETE)
    @PreAuthorize("hasAuthority('btn.menu.remove')")
    @ApiOperation(value = "删除")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable Long id) {
        //验证参数
        if (StringUtils.isEmpty(id)) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        menuService.removeById(id);
        return R.success();
    }

    /**
     * 获取所有
     * @return
     */
    @PreAuthorize("hasAuthority('btn.menu.list')")
    @ApiOperation(value = "获取全部菜单")
    @GetMapping("findMenuInfo")
    public R findMenuInfo() {
        List<MenuVo> menuVoList = menuService.findMenuInfo();
        return R.success(menuVoList);
    }

    /**
     * 同步数据到公众号
     * @return
     */
    @Log(title = "微信菜单管理",businessType = ServiceType.OTHER)
    @PreAuthorize("hasAuthority('btn.menu.syncMenu')")
    @ApiOperation(value = "同步菜单")
    @GetMapping("syncMenu")
    public R syncMenu(){
        menuService.sync();
        return R.success();
    }

    /**
     * 删除菜单
     * @return
     */
    @Log(title = "微信菜单管理",businessType = ServiceType.OTHER)
    @PreAuthorize("hasAuthority('btn.menu.removeMenu')")
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("removeMenu")
    public R removeMenu() {
        menuService.removeMenu();
        return R.success();
    }
}

